home *** CD-ROM | disk | FTP | other *** search
/ SuperHack / SuperHack CD.bin / CODING / DELPHI / FORMFNP.ZIP / FORMFNP.PAS < prev   
Encoding:
Pascal/Delphi Source File  |  1996-08-06  |  8.0 KB  |  291 lines

  1. unit FormFnp;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Registry,
  7.   FnpFormProp;
  8.  
  9. type
  10.   TFnpForm = class(TForm)
  11.   private
  12.     { Private declarations }
  13.     PositionRead: Boolean;
  14.     FAllowMaximized: Boolean;
  15.     FAllowMinimized: Boolean;
  16.     FMinSizeX: Integer;
  17.     FMinSizeY: Integer;
  18.     FMaxSizeX: Integer;
  19.     FMaxSizeY: Integer;
  20.     FRootKey: HKEY;
  21.     FSavePosition: Boolean;
  22.     FSubKey: String;
  23.     FVersion: String;
  24.     procedure DoShow; override;
  25.     procedure WMGetMinMaxInfo(var Msg: TWMGetMinMaxInfo); message WM_GETMINMAXINFO;
  26.     procedure SetAllowMaximized(Value: Boolean);
  27.     procedure SetAllowMinimized(Value: Boolean);
  28.     procedure SetMinSizeX(Value: Integer);
  29.     procedure SetMinSizeY(Value: Integer);
  30.     procedure SetMaxSizeX(Value: Integer);
  31.     procedure SetMaxSizeY(Value: Integer);
  32.     procedure SetRootKey(Value: HKEY);
  33.     procedure SetSavePosition(Value: Boolean);
  34.     procedure SetSubKey(Value: String);
  35.     procedure DoSavePosition;
  36.     procedure DoReadPosition;
  37.   public
  38.     { Public declarations }
  39.     property AllowMaximized: Boolean read FAllowMaximized write SetAllowMaximized;
  40.     property AllowMinimized: Boolean read FAllowMinimized write SetAllowMinimized;
  41.     property MinSizeX: Integer read FMinSizeX write SetMinSizeX;
  42.     property MinSizeY: Integer read FMinSizeY write SetMinSizeY;
  43.     property MaxSizeX: Integer read FMaxSizeX write SetMaxSizeX;
  44.     property MaxSizeY: Integer read FMaxSizeY write SetMaxSizeY;
  45.     property RootKey: HKEY read FRootKey write SetRootKey default HKEY_CURRENT_USER;
  46.     property SavePosition: Boolean read FSavePosition write SetSavePosition;
  47.     property SubKey: String read FSubKey write SetSubKey;
  48.     property Version: String read FVersion;
  49.     constructor Create(AOwner: TComponent); override;
  50.     destructor Destroy; override;
  51.   end;
  52.  
  53. var
  54.   FnpForm: TFnpForm;
  55.  
  56. implementation
  57.  
  58. {$R *.DFM}
  59.  
  60. constructor TFnpForm.Create(AOwner: TComponent);
  61. var
  62.   I: Integer ;
  63.   FFP: TFnpFormProp;
  64. begin
  65.   inherited Create(AOwner);
  66.   FFP := Nil;
  67.   { Does this form contain a TFnpFormProp component? }
  68.   For I := 0 to ComponentCount - 1 do
  69.     if Components[I].ClassName = 'TFnpFormProp' then
  70.     begin
  71.       FFP := TFnpFormProp(Components[I]);
  72.       Break;
  73.     end;
  74.  
  75.   if Assigned(FFP) then
  76.     { Transfer properties from TFnpFormProp }
  77.     begin
  78.       FAllowMaximized := FFP.AllowMaximized;
  79.       FAllowMinimized := FFP.AllowMinimized;
  80.       FMaxSizeX := FFP.MaxSizeX;
  81.       FMaxSizeY := FFP.MaxSizeY;
  82.       FMinSizeX := FFP.MinSizeX;
  83.       FMinSizeY := FFP.MinSizeY;
  84.       if FFP.RootKey = rkCurrentUser then
  85.         FRootKey := HKEY_CURRENT_USER
  86.       else
  87.         FRootKey := HKEY_LOCAL_MACHINE;
  88.       FSavePosition := FFP.SavePosition;
  89.       FSubKey := FFP.SubKey;
  90.       { Don't need it anymore! }
  91.       FFP.Free;
  92.       { We need to handle MDI-child forms specially since DoShow is called
  93.         in the original constructor! Why? }
  94.       if FormStyle = fsMDIChild    then
  95.       begin
  96.         DoReadPosition;
  97.         PositionRead := True;
  98.       end;
  99.     end
  100.   else
  101.     { Set default properties }
  102.     begin
  103.       FAllowMaximized := True;
  104.       FAllowMinimized := True;
  105.       FRootKey := HKEY_CURRENT_USER;
  106.     end;
  107.  
  108.   { Set version }
  109.   FVersion := '1.00.00';
  110.  
  111. end;
  112.  
  113. destructor TFnpForm.Destroy;
  114. begin
  115.   if FSavePosition then
  116.     DoSavePosition;
  117.   inherited;
  118. end;
  119.  
  120. procedure TFnpForm.DoShow;
  121. begin
  122.   inherited DoShow;
  123.   { Do not handle MDI-child forms here, since OnShow is called in the
  124.     original contructor! Why?}
  125.   if FormStyle <> fsMDIChild    then
  126.     if not PositionRead then
  127.     begin
  128.       { Read saved position? }
  129.       if FSavePosition then
  130.         DoReadPosition;
  131.       PositionRead := True;
  132.     end;
  133. end;
  134.  
  135. procedure TFnpForm.WMGetMinMaxInfo(var Msg: TWMGetMinMaxInfo);
  136. begin
  137.   if BorderStyle = bsSizeable then
  138.   begin
  139.     if FMinSizeX > 0 then
  140.       Msg.MinMaxInfo^.ptMinTrackSize.X := FMinSizeX;
  141.     if FMinSizeY > 0 then
  142.       Msg.MinMaxInfo^.ptMinTrackSize.Y := FMinSizeY;
  143.     if FMaxSizeX > 0 then
  144.       Msg.MinMaxInfo^.ptMaxTrackSize.X := FMaxSizeX;
  145.     if FMaxSizeY > 0 then
  146.       Msg.MinMaxInfo^.ptMaxTrackSize.Y := FMaxSizeY;
  147.   end;
  148. end;
  149.  
  150. procedure TFnpForm.SetAllowMaximized(Value: Boolean);
  151. begin
  152.   if FAllowMaximized <> Value then
  153.     FAllowMaximized := Value;
  154. end;
  155.  
  156. procedure TFnpForm.SetAllowMinimized(Value: Boolean);
  157. begin
  158.   if FAllowMinimized <> Value then
  159.     FAllowMinimized := Value;
  160. end;
  161.  
  162. procedure TFnpForm.SetMinSizeX(Value: Integer);
  163. begin
  164.   if FMinSizeX <> Value then
  165.     if Value < 0 then
  166.       FMinSizeX := 0
  167.     else
  168.       FMinSizeX := Value;
  169. end;
  170.  
  171. procedure TFnpForm.SetMinSizeY(Value: Integer);
  172. begin
  173.   if FMinSizeY <> Value then
  174.     if Value < 0 then
  175.       FMinSizeY := 0
  176.     else
  177.       FMinSizeY := Value;
  178. end;
  179.  
  180. procedure TFnpForm.SetMaxSizeX(Value: Integer);
  181. begin
  182.   if FMaxSizeX <> Value then
  183.     if Value < 0 then
  184.       FMaxSizeX := 0
  185.     else
  186.       FMaxSizeX := Value;
  187. end;
  188.  
  189. procedure TFnpForm.SetMaxSizeY(Value: Integer);
  190. begin
  191.   if FMaxSizeY <> Value then
  192.     if Value < 0 then
  193.       FMaxSizeY := 0
  194.     else
  195.       FMaxSizeY := Value;
  196. end;
  197.  
  198. procedure TFnpForm.SetRootKey(Value: HKEY);
  199. begin
  200.   if FRootKey <> Value then
  201.     if (Value = HKEY_CURRENT_USER) or (Value = HKEY_LOCAL_MACHINE) then
  202.       FRootKey := Value;
  203. end;
  204.  
  205. procedure TFnpForm.SetSavePosition(Value: Boolean);
  206. begin
  207.   if FSavePosition <> Value then
  208.     FSavePosition := Value;
  209. end;
  210.  
  211. procedure TFnpForm.SetSubKey(Value: String);
  212. begin
  213.   if FSubKey <> Value then
  214.     FSubKey := Value;
  215. end;
  216.  
  217. procedure TFnpForm.DoReadPosition;
  218. var
  219.   Reg: TRegIniFile;
  220.   Wp: TWINDOWPLACEMENT;
  221.   WinState: TWindowState;
  222. begin
  223.   if Length(FSubKey) > 0 then
  224.   begin
  225.     try
  226.       Reg := TRegIniFile.Create(FSubKey);
  227.       Reg.RootKey := FRootKey;
  228.       Wp.Length := SizeOf(Wp);
  229.       { Is position saved? }
  230.       if Reg.ReadInteger(TForm(Self).Name, 'Left', -9999) <> -9999 then
  231.         begin
  232.           GetWindowPlacement(Handle, @Wp);
  233.           WinState := TWindowState(Reg.ReadInteger(TForm(Self).Name, 'WindowState', 0));
  234.           if not FAllowMaximized and (WinState = wsMaximized) then
  235.             WinState := wsNormal
  236.           else if not FAllowMinimized and (WinState = wsMinimized) then
  237.             WinState := wsNormal;
  238.           Wp.rcNormalPosition.Left := Reg.ReadInteger(TForm(Self).Name, 'Left', Left);
  239.           Wp.rcNormalPosition.Top := Reg.ReadInteger(TForm(Self).Name, 'Top', Top);
  240.           Wp.rcNormalPosition.Right := Reg.ReadInteger(TForm(Self).Name, 'Right', Left + Width);
  241.           Wp.rcNormalPosition.Bottom := Reg.ReadInteger(TForm(Self).Name, 'Bottom', Top + Height);
  242.           case WinState of
  243.             wsNormal:
  244.               Wp.ShowCmd := SW_SHOWNORMAL;
  245.             wsMinimized:
  246.               Wp.ShowCmd := SW_SHOWMINIMIZED;
  247.             wsMaximized:
  248.               Wp.ShowCmd := SW_SHOWMAXIMIZED;
  249.           end;
  250.           SetWindowPlacement(Handle, @Wp);
  251.         end;
  252.     finally
  253.       Reg.Free;
  254.     end;
  255.   end;
  256. end;
  257.  
  258. procedure TFnpForm.DoSavePosition;
  259. var
  260.   Reg: TRegIniFile;
  261.   Wp: TWINDOWPLACEMENT;
  262.   WinState: TWindowState;
  263. begin
  264.   if Length(FSubKey) > 0 then
  265.   begin
  266.     try
  267.       Reg := TRegIniFile.Create(FSubKey);
  268.       Reg.RootKey := FRootKey;
  269.       Wp.Length := SizeOf(Wp);
  270.       GetWindowPlacement(Handle, @Wp);
  271.       Reg.WriteInteger(TForm(Self).Name, 'Left', Wp.rcNormalPosition.Left);
  272.       Reg.WriteInteger(TForm(Self).Name, 'Top', Wp.rcNormalPosition.Top);
  273.       Reg.WriteInteger(TForm(Self).Name, 'Right', Wp.rcNormalPosition.Right);
  274.       Reg.WriteInteger(TForm(Self).Name, 'Bottom', Wp.rcNormalPosition.Bottom);
  275.       case Wp.ShowCmd of
  276.         SW_SHOWNORMAL:
  277.           WinState := wsNormal;
  278.         SW_SHOWMINIMIZED:
  279.           WinState := wsMinimized;
  280.         SW_SHOWMAXIMIZED:
  281.           WinState := wsMaximized;
  282.       end;
  283.       Reg.WriteInteger(TForm(Self).Name, 'WindowState', Integer(WinState));
  284.     finally
  285.       Reg.Free;
  286.     end;
  287.   end;
  288. end;
  289.  
  290. end.
  291.